{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DesignerString;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, PngImageList;

type
  TfrmDesignerString = class(TForm)
    pbHeader: TPaintBox;
    lbCaption: TLabel;
    lbAction: TLabel;
    Bevel1: TBevel;
    btOkay: TButton;
    btCancel: TButton;
    edString: TEdit;
    lbString: TLabel;
    procedure edStringChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btOkayClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure pbHeaderPaint(Sender: TObject);
  private
    { Private declarations }
    FTextMsg: String;
    FHelpMsg: String;
    FStringMsg: String;
    FStringText: String;
    FHeaderImages: TPngImageList;
    procedure ConfigureForm;
    procedure ValidateForm;
    procedure LoadImages;
  public
    { Public declarations }
    class function EditString(const Caption, TextMsg, HelpMsg, StringMsg: String; var Text: String): Boolean;
    property TextMsg: String read FTextMsg write FTextMsg;
    property HelpMsg: String read FHelpMsg write FHelpMsg;
    property StringMsg: String read FStringMsg write FStringMsg;
    property StringText: String read FStringText write FStringText;
  end;


implementation

{$R *.dfm}

var
  FormInstance: TfrmDesignerString;

//-----------------------------------------------------------------------------
//----------------------------- Class Functions -------------------------------
//-----------------------------------------------------------------------------

class function TfrmDesignerString.EditString(const Caption, TextMsg, HelpMsg, StringMsg: String;
                                             var Text: String): Boolean;
begin
  // Set default result - unchanged
  Result := False;

  // Create a singleton form instance
  if not Assigned(FormInstance) then
  begin
    FormInstance := TfrmDesignerString.Create(Application.MainForm);
    FormInstance.PopupParent := Application.MainForm;
  end;

  FormInstance.Caption := Caption;
  FormInstance.TextMsg := TextMsg;
  FormInstance.HelpMsg := HelpMsg;
  FormInstance.StringMsg := StringMsg;
  FormInstance.StringText := Text;

  if FormInstance.ShowModal = mrOK then
  begin
    Text := FormInstance.StringText;
    Result := True;
  end;
end;

procedure TfrmDesignerString.edStringChange(Sender: TObject);
begin
  ValidateForm;
end;

//-----------------------------------------------------------------------------
//----------------------------- Form Events -----------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerString.FormCreate(Sender: TObject);
begin
  // Create and populate PNG image list
  FHeaderImages := TPngImageList.Create(Self);
  LoadImages;
end;

procedure TfrmDesignerString.FormShow(Sender: TObject);
begin
  ConfigureForm;
end;

//-----------------------------------------------------------------------------
//----------------------------- Control Events --------------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerString.pbHeaderPaint(Sender: TObject);
begin
  with pbHeader do
  begin
    // Draw background
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(GetClientRect);
    // Draw appropriate header image
    FHeaderImages.Draw(Canvas, ClientWidth - FHeaderImages.Width, 0, 0);
  end;
end;

procedure TfrmDesignerString.btOkayClick(Sender: TObject);
begin
  FStringText := edString.Text;
  ModalResult := mrOK;
end;

//-----------------------------------------------------------------------------
//-------------------------- User Interface Update ----------------------------
//-----------------------------------------------------------------------------

procedure TfrmDesignerString.ConfigureForm;
begin
  lbCaption.Caption := FTextMsg;
  lbAction.Caption := FHelpMsg;
  lbString.Caption := FStringMsg;
  edString.Text := FStringText;

  ValidateForm;

  // Force edit to regain focus so selection is highlighted
  btCancel.SetFocus;
  edString.SetFocus;
end;

procedure TfrmDesignerString.ValidateForm;
var
  Valid: Boolean;
begin
  // Ensure user has entered some text
  Valid := Trim(edString.Text) <> '';

  btOkay.Enabled := Valid;
end;

procedure TfrmDesignerString.LoadImages;
begin
  with FHeaderImages do
  begin
    Clear;
    Width := 150; Height := 57;

    AddResourcePng('HD_IMG_GENERIC'); //0
  end;
end;



end.
